using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace Lyh.Train.Common.Http
{
	public class HtmlHelper
	{
		public static string GetElementById(string html, string id)
		{
			string text = "<([a-z]+)(?:(?!id)[^<>])*id=([\"']?){0}\\2[^>]*>(?>(?<o><\\1[^>]*>)|(?<-o></\\1>)|(?:(?!</?\\1).))*(?(o)(?!))</\\1>";
			text = string.Format(text, Regex.Escape(id));
			Match match = Regex.Match(html, text, RegexOptions.IgnoreCase | RegexOptions.Singleline);
			return match.Success ? match.Value : "";
		}
		public static string[] GetElementsByClass(string html, string className)
		{
			return HtmlHelper.GetElements(html, "", className);
		}
		public static string[] GetElementsByTagName(string html, string tagName)
		{
			return HtmlHelper.GetElements(html, tagName, "");
		}
		public static string[] GetElementsByTagAndClass(string html, string tagName, string className)
		{
			return HtmlHelper.GetElements(html, tagName, className);
		}
		private static string[] GetElements(string html, string tagName, string className)
		{
			string text = "";
			if (tagName != "" && className != "")
			{
				text = "<({0})(?:(?!class)[^<>])*class=([\"']?){1}\\2[^>]*>(?>(?<o><\\1[^>]*>)|(?<-o></\\1>)|(?:(?!</?\\1).))*(?(o)(?!))</\\1>";
				text = string.Format(text, Regex.Escape(tagName), Regex.Escape(className));
			}
			else
			{
				if (tagName != "")
				{
					text = "<({0})(?:[^<>])*>(?>(?<o><\\1[^>]*>)|(?<-o></\\1>)|(?:(?!</?\\1).))*(?(o)(?!))</\\1>";
					text = string.Format(text, Regex.Escape(tagName));
				}
				else
				{
					if (className != "")
					{
						text = "<([a-z]+)(?:(?!class)[^<>])*class=([\"']?){0}\\2[^>]*>(?>(?<o><\\1[^>]*>)|(?<-o></\\1>)|(?:(?!</?\\1).))*(?(o)(?!))</\\1>";
						text = string.Format(text, Regex.Escape(className));
					}
				}
			}
			string[] result;
			if (text == "")
			{
				result = new string[0];
			}
			else
			{
				List<string> list = new List<string>();
				Regex regex = new Regex(text, RegexOptions.IgnoreCase | RegexOptions.Singleline);
				Match match = regex.Match(html);
				while (match.Success)
				{
					list.Add(match.Value);
					match = regex.Match(html, match.Index + match.Length);
				}
				result = list.ToArray();
			}
			return result;
		}
		public static List<string> GetListByHtml(string text, string pat)
		{
			List<string> list = new List<string>();
			Regex regex = new Regex(pat, RegexOptions.IgnoreCase);
			Match match = regex.Match(text);
			while (match.Success)
			{
				list.Add(match.Value);
				match = match.NextMatch();
			}
			return list;
		}
		public static string GetResultByHtml(string text, string pat)
		{
			string result = string.Empty;
			Regex regex = new Regex(pat, RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline);
			Match match = regex.Match(text);
			if (match.Success)
			{
				result = match.Value;
			}
			return result;
		}
		public static string NoHTML(string Htmlstring)
		{
			Htmlstring = Regex.Replace(Htmlstring, "<script[^>]*?>.*?</script>", "", RegexOptions.IgnoreCase);
			Htmlstring = Regex.Replace(Htmlstring, "<(.[^>]*)>", "", RegexOptions.IgnoreCase);
			Htmlstring = Regex.Replace(Htmlstring, "([\\r\\n])[\\s]+", "", RegexOptions.IgnoreCase);
			Htmlstring = Regex.Replace(Htmlstring, "-->", "", RegexOptions.IgnoreCase);
			Htmlstring = Regex.Replace(Htmlstring, "<!--.*", "", RegexOptions.IgnoreCase);
			Htmlstring = Regex.Replace(Htmlstring, "&(quot|#34);", "\"", RegexOptions.IgnoreCase);
			Htmlstring = Regex.Replace(Htmlstring, "&(amp|#38);", "&", RegexOptions.IgnoreCase);
			Htmlstring = Regex.Replace(Htmlstring, "&(lt|#60);", "<", RegexOptions.IgnoreCase);
			Htmlstring = Regex.Replace(Htmlstring, "&(gt|#62);", ">", RegexOptions.IgnoreCase);
			Htmlstring = Regex.Replace(Htmlstring, "&(nbsp|#160);", "   ", RegexOptions.IgnoreCase);
			Htmlstring = Regex.Replace(Htmlstring, "&(iexcl|#161);", "¡", RegexOptions.IgnoreCase);
			Htmlstring = Regex.Replace(Htmlstring, "&(cent|#162);", "¢", RegexOptions.IgnoreCase);
			Htmlstring = Regex.Replace(Htmlstring, "&(pound|#163);", "£", RegexOptions.IgnoreCase);
			Htmlstring = Regex.Replace(Htmlstring, "&(copy|#169);", "©", RegexOptions.IgnoreCase);
			Htmlstring = Regex.Replace(Htmlstring, "&#(\\d+);", "", RegexOptions.IgnoreCase);
			Htmlstring.Replace("<", "");
			Htmlstring.Replace(">", "");
			Htmlstring = Htmlstring.Trim();
			return Htmlstring;
		}
		public static string[] GetHtmlImageUrlList(string sHtmlText)
		{
			Regex regex = new Regex("<img\\b[^<>]*?\\bsrc[\\s\\t\\r\\n]*=[\\s\\t\\r\\n]*[\"']?[\\s\\t\\r\\n]*(?<imgUrl>[^\\s\\t\\r\\n\"'<>]*)[^<>]*?/?[\\s\\t\\r\\n]*>", RegexOptions.IgnoreCase);
			MatchCollection matchCollection = regex.Matches(sHtmlText);
			int num = 0;
			string[] array = new string[matchCollection.Count];
			foreach (Match match in matchCollection)
			{
				array[num++] = match.Groups["imgUrl"].Value;
			}
			return array;
		}
		public static string[] GetImagAltList(string strHtml)
		{
			Regex regex = new Regex("<img\\b[^<>]*?\\balt[\\s\\t\\r\\n]*=[\\s\\t\\r\\n]*[\"']?[\\s\\t\\r\\n]*(?<imgAlt>[^\\s\\t\\r\\n\"'<>]*)[^<>]*?/?[\\s\\t\\r\\n]*>", RegexOptions.IgnoreCase);
			MatchCollection matchCollection = regex.Matches(strHtml);
			int num = 0;
			string[] array = new string[matchCollection.Count];
			foreach (Match match in matchCollection)
			{
				array[num++] = match.Groups["imgAlt"].Value;
			}
			return array;
		}
		public static string GetTagContent(string str, string tag)
		{
			string pattern = string.Format("<{0}[^>]*?>(?<Text>[^<]*)</{1}>", tag, tag);
			Match match = Regex.Match(str, pattern, RegexOptions.IgnoreCase);
			return match.Groups["Text"].Value;
		}
		public static string GetTagContent(string str, string tag, string attrib)
		{
			string pattern = string.Format("<{0}[^>]*?{1}=(['\"\"]?)(?<url>[^'\"\"\\s>]+)\\1[^>]*>", tag, attrib);
			Match match = Regex.Match(str, pattern, RegexOptions.IgnoreCase);
			return match.Groups["url"].Value;
		}
		public static string GetContent(string str, string startTag, string endTag)
		{
			string result = string.Empty;
			string pattern = string.Concat(new string[]
			{
				"(?<=",
				startTag,
				")[\\s\\S]*?(?=",
				endTag,
				")"
			});
			Regex regex = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline);
			Match match = regex.Match(str);
			if (match.Success)
			{
				result = match.Value;
			}
			return result;
		}
	}
}
